#include <Adafruit_ssd1306syp.h>
#include "DHT.h" //加载DHT11的库
#define DHTTYPE DHT11 // 定义传感器类似 DHT11
#define DHTPIN 12 

#define SDA_PIN 11                 
#define SCL_PIN 10

#define HUM_A A0  
#define HUM_D 8     //温湿度

int potPin = A1;  //光照强度

#define Motor 9   //抽水
#define Motor2 7  //绿灯
#define Motor3 6  //白灯

//宏定义DHT数据接口，编译时DHTPIN会替换成2
DHT dht(DHTPIN, DHTTYPE);//声明 dht 函数
Adafruit_ssd1306syp display(SDA_PIN,SCL_PIN);
void setup() {  
  pinMode(HUM_A, INPUT);
  pinMode(HUM_D, INPUT);  
  pinMode(Motor, OUTPUT);
  pinMode(Motor2, OUTPUT);
  pinMode(Motor3, OUTPUT);
  Serial.begin(115200);  
  display.initialize();
  dht.begin(); //启动传感器
}  
 
void loop() {
  float h = dht.readHumidity();//读取湿度
  float t = dht.readTemperature();//读取摄氏度
  if (isnan(h) || isnan(t) ) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  } //检查抓取是否成功

  int sen=analogRead(HUM_A);
  
  int val = analogRead(potPin);
  Serial.println(val);
  if(val >600 && val< 670) {
     digitalWrite(Motor3, HIGH);  
  }else if(val>670){
     digitalWrite(Motor2, HIGH);  
  }else{
     digitalWrite(Motor2, LOW);  
     digitalWrite(Motor3, LOW);    
  }
    
  Serial.print("AO=");  
  Serial.print(sen);
  Serial.print(", DO="); 
  
  if(sen >=600) {   //开始抽水
    digitalWrite(Motor, LOW);  
    }
  if(sen<=500){     //停止抽水
     digitalWrite(Motor, HIGH);  
    }
  Serial.println(digitalRead(HUM_D));
  Oled_display(sen,h,t);
  delay(1000);  
}

void Oled_display(int hum,int h,int t) {
  display.clear();
  display.setCursor(0,0);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.print("s_hum:");
  display.println(hum);
    display.print("w_hum:");
  display.println(h);
    display.print("w_tem:");
  display.println(t);
  display.update();
 
  
}
